﻿using EasyShop.OrderService.App.Interface;
using EasyShop.OrderService.Request;
using EasyShop.OrderService.Response;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace EasyShop.OrderService.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly IOrderApp _orderApp;
        public OrderController(IOrderApp orderApp)
        {
            _orderApp = orderApp;
        }
        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public Response<OrderListView> GetList(string userId)
        {
            var result = new Response<OrderListView>();
            try
            {
                result.Result = _orderApp.GetList(userId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 购买商品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public Response<string> Buy([FromBody] BuyReq request)
        {
            var result = new Response<string>();
            try
            {
                result.Result = _orderApp.Buy(request);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public Response<string> Delete(string orderId)
        {
            var result = new Response<string>();
            try
            {
                result.Result = _orderApp.Delete(orderId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }

}
